package cn.yhjz.platform.system.vo;

import cn.yhjz.platform.system.entity.LogedUserDetails;
import cn.yhjz.platform.system.entity.SysUser;
import cn.hutool.core.collection.CollUtil;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 登录用户
 */
@Data
public class LogedUser implements Serializable {

    private Long id;
    private String userCode;
    private String username;
    private String nickname;
    private String avatarFileCode;
    private String avatarFileHashCode;
    private Date lastLoginTime;

    //用户的权限字符串列表
    private List<String> permissionList;

    //用户的菜单
    private List<SysMenuVo> menuTree;

    public static LogedUser fromSecurityUser(SysUser sysUser, LogedUserDetails securityUser) {
        LogedUser logedUser = new LogedUser();
        logedUser.setId(sysUser.getId());
        logedUser.setUserCode(sysUser.getUserCode());
        logedUser.setUsername(sysUser.getUsername());
        logedUser.setAvatarFileCode(sysUser.getAvatarFileCode());
        logedUser.setAvatarFileHashCode(sysUser.getAvatarFileHash());
        logedUser.setNickname(sysUser.getNickname());
        logedUser.setPermissionList(new ArrayList<>());
        if (CollUtil.isNotEmpty(securityUser.getAuthorities())) {
            for (GrantedAuthority authority : securityUser.getAuthorities()) {
                logedUser.getPermissionList().add(authority.getAuthority());
            }
        }
        return logedUser;
    }


}
